﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Annual_faculty_promotions.Core.Domain;
using Annual_faculty_promotions.Data;
using Annual_faculty_promotions.Service.Contracts;
using Annual_faculty_promotions.WebUI.Areas.UserArea.Models;
using Annual_faculty_promotions.WebUI.Helpers.Filters;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Action = Antlr.Runtime.Misc.Action;

namespace Annual_faculty_promotions.WebUI.Areas.UserArea.Controllers
{
    public class StageController : Controller
    {
        private readonly IStageService _stageService;
        private readonly IUnivercityStructureService _univercityStructure;
        private readonly IUserService _userService;
        private readonly IApplicationRoleManager _roleManager;
        private readonly IUnitOfWork _unitOfWork;

        public StageController(IUnitOfWork unitOfWork,
            IStageService stageService,
            IApplicationRoleManager roleManager,
            IUserService userService,
            IUnivercityStructureService univercityStructure)
        {
            _unitOfWork = unitOfWork;
            _stageService = stageService;
            _roleManager = roleManager;
            _userService = userService;
            _univercityStructure = univercityStructure;
        }
        //
        // GET: /UserArea/Stage/
        public ActionResult Index()
        {

            return View();
        }

        public ActionResult GetStageNumberToJson()
        {
            var sizes = _stageService.GetAllStages();
            var serializer = new JavaScriptSerializer();
            JArray ja = new JArray();

            foreach (var item in sizes)
            {
                var itemObject = new JObject
                {
                    {"StageId",item.Id},
                    {"StageNumber",item.StageNumber}
                };
                ja.Add(itemObject);
            }
            return Content(JsonConvert.SerializeObject(ja), "application/json");
        }

        public ActionResult GetRoleJason()
        {
            var sizes = _roleManager.GetAllCustomRolesAsQueryable();
            JArray ja = new JArray();

            foreach (var item in sizes)
            {
                var itemObject = new JObject
                {
                    {"Id",item.Id},
                    {"RoleName",item.Name}
                };
                ja.Add(itemObject);
            }
            return Content(JsonConvert.SerializeObject(ja), "application/json");
        }

        public ActionResult GetUserJson()
        {
            var user = _userService.Where(x=>x.Profile!=null).Include(x => x.Profile).ToList();  

            JArray ja = new JArray();

            foreach (var item in user)
            {
                var itemObject = new JObject
                {
                    {"UserId",item.Id},
                    {"UserName",item.Profile.Name+" "+item.Profile.Family}
                };
                ja.Add(itemObject);
            }
            return Content(JsonConvert.SerializeObject(ja), "application/json");
        }


        public ActionResult GetDepartmentJson()
        {
            var structures = _univercityStructure.GetAllUnivercityStructures();

            JArray ja = new JArray();

            foreach (var item in structures)
            {
                var itemObject = new JObject
                {
                    {"StructId",item.Id},
                    {"StructName",item.Name}
                };
                ja.Add(itemObject);
            }
            return Content(JsonConvert.SerializeObject(ja), "application/json"); 
        }



        public ActionResult GetStages(int page = 1, int pageSize = 17)
        {
            var items = _stageService.GetAllStagesAsQueryable().Include(c => c.Role)
                .OrderBy(x => x.Id)
                .Skip((page - 1) * pageSize)
                .Take(pageSize);

            JArray ja = new JArray();

            foreach (var item in items)
            {
                var itemObject = new JObject
                {
                    {"Id",item.Id},
                    {"RoleName",item.Role.Name},
                    {"Name",item.Name},
                    {"StageNumber",item.StageNumber},
                     {"Active",item.Active}
                };
                ja.Add(itemObject);
            }

            JObject jo = new JObject();
            jo.Add("total", _stageService.GetAllStagesAsQueryable().Count());
            jo.Add("rows", ja);
            return Content(JsonConvert.SerializeObject(jo), "application/json");
        }

        [HttpPost]
        [AjaxOnly]
        public ActionResult Create(Stage stage)
        {
            if (ModelState.IsValid)
            {
                _stageService.AddNewStage(stage);
                _unitOfWork.SaveChanges();
                return Json(new { Success = true, Msg = "ثبت شد" });
            }
            return Json(new { Success = false, Msg = "خطا در ثبت!" });
        }


        [HttpPost]
        [AjaxOnly]
        public ActionResult Delete(int id)
        {
            try
            {
                var model = _stageService.Find(id);
                if (model == null)
                    return HttpNotFound();
                _stageService.Delete(id);
                _unitOfWork.SaveChanges();
                return Json(new { success = true, Msg = "حذف شد" });
            }
            catch (Exception)
            {

                return Json(new { success = false, Msg = "حذف نشد" });
            }
        }


        public ActionResult Edit(int? stageId)
        {
            if (stageId == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var stage = _stageService.Find(stageId);
            if (stage == null)
            {
                return HttpNotFound();
            }
            return PartialView("_Edit", stage);
        }

        [HttpPost]
        public ActionResult Edit(Stage stage)
        {
            if (ModelState.IsValid)
            {
                _stageService.EditStage(stage);
                _unitOfWork.SaveChanges();
                return Json(new { success = true, Msg = "تغییرات اعمال شد" });
            }
            return Json(new { success = false, Msg = "ورودی نامعتبر" });
        }

       
        public ActionResult GetStageJason()
        {
            var sizes = _stageService.GetAllStages();
            JArray ja = new JArray();
            foreach (var item in sizes)
            {
                var itemObject = new JObject
                {
                    {"StageId",item.Id},
                    {"StageName",item.Name}
                };
                ja.Add(itemObject);
            }
            return Content(JsonConvert.SerializeObject(ja), "application/json");
        }

    }
}